package com.phiture.erp.basic.core.service.impl;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.phiture.erp.basic.core.pojo.vo.ErpUserProcessMappingPageReqVO;
import com.phiture.erp.basic.core.pojo.vo.ErpUserProcessMappingSaveReqVO;
import com.phiture.erp.basic.core.service.ErpUserProcessMappingService;
import com.phiture.erp.basic.dal.entity.ErpUserProcessMappingDO;
import com.phiture.erp.basic.dal.mapper.ErpUserProcessMappingMapper;
import com.phiture.erp.basic.dal.pojo.vo.ErpUserProcessMappingRespVO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.phiture.erp.common.constants.ErrorCodeConstants.USER_PROCESS_MAPPING_NOT_EXISTS;

/**
 * 人员工序对照 Service 实现类
 *
 * @author lekaihuai
 */
@Service
@Validated
public class ErpUserProcessMappingServiceImpl implements ErpUserProcessMappingService {

    @Resource
    private ErpUserProcessMappingMapper userProcessMappingMapper;

    @Override
    public Long createUserProcessMapping(ErpUserProcessMappingSaveReqVO createReqVO) {
        // 插入
        ErpUserProcessMappingDO userProcessMapping = BeanUtils.toBean(createReqVO, ErpUserProcessMappingDO.class);
        userProcessMappingMapper.insert(userProcessMapping);
        // 返回
        return userProcessMapping.getId();
    }

    @Override
    public void updateUserProcessMapping(ErpUserProcessMappingSaveReqVO updateReqVO) {
        // 校验存在
        validateUserProcessMappingExists(updateReqVO.getId());
        // 更新
        ErpUserProcessMappingDO updateObj = BeanUtils.toBean(updateReqVO, ErpUserProcessMappingDO.class);
        userProcessMappingMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserProcessMapping(Long id) {
        // 校验存在
        validateUserProcessMappingExists(id);
        // 删除
        userProcessMappingMapper.deleteById(id);
    }

    private void validateUserProcessMappingExists(Long id) {
        if (userProcessMappingMapper.selectById(id) == null) {
            throw exception(USER_PROCESS_MAPPING_NOT_EXISTS);
        }
    }

    @Override
    public ErpUserProcessMappingDO getUserProcessMapping(Long id) {
        return userProcessMappingMapper.selectById(id);
    }

    @Override
    public PageResult<ErpUserProcessMappingRespVO> getUserProcessMappingPage(ErpUserProcessMappingPageReqVO pageReqVO) {
        IPage<ErpUserProcessMappingRespVO> pageResult =
                userProcessMappingMapper.selectMyPage(MyBatisUtils.buildPage(pageReqVO),
                        pageReqVO.getUserId(),
                        pageReqVO.getProcessId(),
                        TenantContextHolder.getTenantId());
        return new PageResult<>(pageResult.getRecords(), pageResult.getTotal());
    }

}